package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.ChartDeleteDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartValueSaveDTO;
import com.xbongbong.pro.statistic.pojo.vo.ChartDeleteVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartEditVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartSaveVO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultVO;
import com.xbongbong.pro.statistic.service.ChartCustomService;
import com.xbongbong.pro.statistic.service.ChartService;
import com.xbongbong.pro.statistic.through.pojo.dto.ResultThroughDTO;
import com.xbongbong.pro.statistic.through.pojo.vo.IndexThroughDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 自定义pk相关接口
 * @author xinpeng.jiang
 * @date 2021/4/27 9:22
 * @version 1.0
 * @since V1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/pk/custom")
public class PkCustomController {
    private static final Logger LOG = LoggerFactory.getLogger(PkCustomController.class);
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private ChartService chartService;
    @Resource
    private ChartCustomService chartCustomService;

    /**
     * 自定义pk指标的保存
     * @param chartValueSaveDTO  入参
     * @param br  异常捕获
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid ChartValueSaveDTO chartValueSaveDTO, BindingResult br) {
        XbbResponse<ChartSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // ----------------------------------repeat post----------------------------------------------
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(chartValueSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(chartValueSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                // ----------------------------------repeat post----------------------------------------------
                // 自定义pk默认参数处理
                chartValueSaveDTO.getChart().customPkSaveFormat();
                ChartSaveVO chartSaveVO = chartService.chartValueSave(chartValueSaveDTO);
                response = new XbbResponse<>(chartSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 编辑指标
     * @param chartEditDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartEdit(@RequestBody @Valid ChartEditDTO chartEditDTO, BindingResult br) {
        XbbResponse<ChartEditVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartEditVO chartEditVO = chartService.chartEdit(chartEditDTO);
                response = new XbbResponse<>(chartEditVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 指标删除
     * @param chartDeleteDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @throws XbbException
     * @version v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartDelete(@RequestBody @Valid ChartDeleteDTO chartDeleteDTO, BindingResult br) {
        XbbResponse<ChartDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartDeleteVO chartDeleteVO = chartService.chartDelete(chartDeleteDTO);
                response = new XbbResponse<>(chartDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 自定义pk图表中心穿透 web
     * @param resultThroughDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2021/5/11 19:40
     * @version 1.0
     */
    @RequestMapping(value = "/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customChartResultThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br) {
        XbbResponse<IndexThroughDTO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexThroughDTO indexThroughDTO = chartCustomService.customPkResultThrough(resultThroughDTO);
                response = new XbbResponse<>(indexThroughDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 自定义pk图表中心穿透 app
     * @param resultThroughDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2021/5/11 19:40
     * @version 1.0
     */
    @RequestMapping(value = "/mobile/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customChartResultThrough4Mobile(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br) {
        XbbResponse<ChartResultVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartResultVO chartResultVO = chartCustomService.customPkMobileResultThrough(resultThroughDTO);
                response = new XbbResponse<>(chartResultVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
